///////////////////
//BLOCK FONCTIONS METIER
///////////////////
//function init_commun()
//function show_commun()
//function getUserCode() 

///////////////////
//BLOCK FONCTIONS PRESENTATION
///////////////////

///////////////////
//BLOCK FONCTIONS TECHNIQUES
///////////////////
//function checkAuth()

///////////////////
//BLOCK FONCTIONS AJAX JS/PHP/MYSQL
///////////////////
//function getAuth(p_cle) 


//---------------------------------------------------------

///////////////////
//BLOCK Variable globale
///////////////////

///////////////////
//BLOCK FONCTIONS METIER
///////////////////
function init_commun(){
	try {
		// Init commun
		checkMaintenance();
		
		auth = checkAuth();
		tabInfoLogin = getAuthInfo(auth);
		//[0][0] = code_user
		//[0][1] = libelle_user
		//[0][2] = description
		//[0][3] = montrer_aide_ihm
		//[0][4] = rang
	
		var html = getHtmlMenu(tabInfoLogin[0][3]);
		$("#mypanel").append(html).trigger('create');
	}
	catch (er) {
		log(0, "ERROR(init_commun):" + er.message);
	}
}

function show_commun() {
	try {
		//Ajoute titre page
		if ( typeof id_page !== "undefined" ) {
			var strSQL = "SELECT Description FROM `tab_menu` WHERE 1=1 AND `id` = "+id_page+";";
			var titre = getSQL(strSQL)[0][0];
			
			document.title = getParameter("nom_site") + " - " + titre;
			
			$("#id_titre").text(titre);
		}else{
			document.title = getParameter("nom_site") + " - " + document.title;
		}
	}
	catch (er) {
		log(0, "ERROR(show_commun):" + er.message);
	}
}


function getUserCode() {
    try {
		return get_cookie("user");
    }
    catch (er) {
        log(0, "ERROR(getUserCode):" + er.message);
		return "";
    }
}

///////////////////
//BLOCK FONCTIONS PRESENTATION
///////////////////

///////////////////
//BLOCK FONCTIONS TECHNIQUES
///////////////////
function checkAuth() {
    try {
		var pageName = location.pathname.substring(location.pathname.lastIndexOf("/") + 1);
		if(pageName == ""){
			pageName = "index.html";
		}
		var tabUser = new Array();
		var strAuthName = "";
        strAuthName = get_cookie("user");
        if (strAuthName == "") {
			var get_cle = "";
            get_cle = getParamGet("cle");
			if(get_cle != ""){
                tabUser = getAuth(get_cle);
				if(tabUser.length > 0){
					var user = tabUser[0][0];
					set_cookie("user",user);
					addStat(user,pageName,"checkAuth : ok");
					window.location = "page_home.html?mili="+getMilise();
				}else{
					addStat("INC",pageName,"checkAuth : ko("+get_cle+")");
					window.location = "page_error.html?mili="+getMilise();
				}
			}else{
				addStat("INC",pageName,"checkAuth : ko("+get_cle+")");
				window.location = "page_error.html?mili="+getMilise();
			}
        }else{
			var get_cle = "";
            get_cle = getParamGet("cle");
			if(get_cle != ""){
				tabUser = getAuth(get_cle);
				if(tabUser.length > 0){
					var user = tabUser[0][0];
					set_cookie("user",user);
					addStat(user,pageName,"checkAuth : ok");
					var url = window.location.pathname;
					var tabUrl = new Array();
					tabUrl = url.split('/');
					var page = "";
					page = tabUrl[tabUrl.length - 1];
					if ((page == "index.html")||(page == "")){
						window.location = "page_home.html?mili="+getMilise();
					}else{
						var myObj_AffichageUser = document.getElementById("id_affichageUser");
						myObj_AffichageUser.innerHTML = "" + tabUser[0][1] + "";
						if(tabUser[0][3] == "1"){
							afficheAideIhm(tabUser[0][0]);
						}
					}
				}else{
					addStat("INC",pageName,"checkAuth : ko("+get_cle+")");
					window.location = "page_error.html?mili="+getMilise();
				}
			}else{
				addStat(strAuthName, pageName, "checkAuth : ok");
				tabUser = getAuthInfo(strAuthName);
				var url = window.location.pathname;
				var tabUrl = new Array();
				tabUrl = url.split('/');
				var page = "";
				page = tabUrl[tabUrl.length - 1];
				if ((page == "index.html")||(page == "")){
					window.location = "page_home.html?mili="+getMilise();
				}else{
					var myObj_AffichageUser = document.getElementById("id_affichageUser");
					myObj_AffichageUser.innerHTML = "" + tabUser[0][1] + "";
					if(tabUser[0][6] == "1"){
						afficheAideIhm(tabUser[0][0]);
					}
				}
			}
		}

        return strAuthName;
    }
    catch (er) {
        log(0, "ERROR(checkAuth):" + er.message);
        return "Error";
    }
}

///////////////////
//BLOCK FONCTIONS AJAX JS/PHP/MYSQL
///////////////////
function getAuth(p_cle) {
	//[0][0] = code_user
	//[0][1] = libelle_user
	//[0][2] = description
	//[0][3] = montrer_aide_ihm
	try {
		var strResponse = "";
		var returns = new Array();
		var tempTab = new Array();
		var tempSubTab = new Array();

		xhr_object = new AJ();

		var url = "phpsql/mysql_getAuth.php?milis=" + getMilise() + "&cle=" + p_cle;

		xhr_object.open("GET", url, false);
		xhr_object.send(null);
		if (xhr_object.readyState == 4) {
			strResponse = xhr_object.responseText;
		} else {
			strResponse = "ERROR";
		}

		tempTab = strResponse.split("\n");

		for (var indice in tempTab) {
			tempSubTab = tempTab[indice].split("&");
			if (tempSubTab.length > 1) {
				returns[returns.length] = tempSubTab.slice(0, tempSubTab.length - 1);
			}
		}

		delete xhr_object;

		return returns;
	}
	catch (er) {
		log(0, "ERROR(getAuth):" + er.message);
	}
}